<template>

  <div class="level">
    <h1>医院</h1>
    <div class="content">
      <div class="left">
        等级:
      </div>
      <ul class="hospital">
        <li :class="{active:activeFlag==''}" @click="changeLevel('')">全部</li>
        <li v-for="level in levelArr" :class="{active:activeFlag==level.value}" :key="level.value"
            @click="changeLevel(level.value)">{{ level.name }}
        </li>

      </ul>

    </div>

  </div>
</template>


<script setup lang="ts">
//引入这个请求的方法
import {reqHospitalLevelAndRegion} from '@/api/home'
//引入组合式api函数
import {onMounted, ref} from "vue";
import {HospitalLevelAndRegionResponseData, HospitalLevelAndRegionArr} from "@/api/home/type.ts";

let activeFlag = ref<string>('');
//通过数组存储医院等级
let levelArr = ref<HospitalLevelAndRegionArr>([]);
//组件挂载完毕
onMounted(() => {

  //组件一挂载完毕，就获取一次医院等级的数据
  getLevel();
});

//点击等级的按钮回调
const changeLevel = (level: string) => {

  activeFlag.value = level;

  //触发自定义事件：将医院等级参数回传给父组件

  $emit('getLevel',level);

}


//获取医院等级的数据
const getLevel = async () => {

  let result: HospitalLevelAndRegionResponseData = await reqHospitalLevelAndRegion('HosType');
  //存储医院等级数据
  if (result.data.code === 200) {
    levelArr.value = result.data.data;

  }


};
//父子组件的通信，子组件往父组件传递参数
let $emit=defineEmits(['getLevel']);


</script>

<script lang="ts">
export default {
  name: "Level"
}
</script>


<style scoped lang="scss">
.level {

  color: #7f7f7f;

  h1 {

    font-weight: 900;
    margin: 10px 0px;
  }

  .content {
    display: flex;

    .left {
      margin-right: 10px;
    }

    .hospital {
      display: flex;

      li {
        margin-right: 10px;

        &.active {
          color: #55a6fe;
        }

      }

      li:hover {
        color: #55a6fe;
        cursor: pointer;
      }
    }
  }
}

</style>
